package com.spring.springcloud.Elasticsearch;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.settings.Settings;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class A_ES的基本操作 {
    public static void main(String[] args) throws IOException {

        //在 Elasticsearch  8.x的版本已过期
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        );
        String sy = "user";//对此索引操作
//        创建索引(esClient,sy);
//        查询索引(esClient,sy);
//        删除索引(esClient,sy);



        esClient.close();//关闭客户端
    }

    private static void 删除索引(RestHighLevelClient esClient,String sy) throws IOException {
        //删除索引
        DeleteIndexRequest dr = new DeleteIndexRequest(sy);
        AcknowledgedResponse delete =
                esClient.indices().delete(dr, RequestOptions.DEFAULT);

        System.out.println("删除索引：" + delete.isAcknowledged());
    }

    private static void 查询索引(RestHighLevelClient esClient,String sy) throws IOException {
        //查询索引
        GetIndexRequest gr = new GetIndexRequest(sy);
        GetIndexResponse getIndexResponse =
                esClient.indices().get(gr, RequestOptions.DEFAULT);

        Map<String, List<AliasMetadata>> aliases = getIndexResponse.getAliases();
        Map<String, MappingMetadata> mappings = getIndexResponse.getMappings();
        Map<String, Settings> settings = getIndexResponse.getSettings();

        System.out.println("***************************************************");
        System.out.println("--------aliases-------------------------------------");
        System.out.println(aliases);
        System.out.println("--------mappings-------------------------------------");
        System.out.println(mappings);
        System.out.println("--------settings-------------------------------------");
        System.out.println(settings);
        System.out.println("***************************************************");
    }

    private static void 创建索引(RestHighLevelClient esClient,String sy) throws IOException {
        //创建索引   创建一个user的索引
        CreateIndexRequest user = new CreateIndexRequest(sy);

        CreateIndexResponse createIndexResponse =
                esClient.indices().create(user, RequestOptions.DEFAULT);

        boolean acknowledged = createIndexResponse.isAcknowledged();

        System.out.println("创建索引：" + acknowledged);
    }
}
